package com.project.picxg.util;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class DownloaderFile {

    private static FileInputStream fis = null;
    private static BufferedInputStream bis = null;
    private static OutputStream os = null;
    private static BufferedOutputStream bos = null;


    public static void SetInputOrOutputStream(File f, HttpServletResponse response) throws IOException {
        fis = new FileInputStream(f);
        bis = new BufferedInputStream(fis);
        os = response.getOutputStream();
        bos = new BufferedOutputStream(os);
    }

    //推流上传
    public static void uploadReport(File f){
        try {
            // 文件是否存在
            if (f.exists()) {
                byte[] b = new byte[1024];
                int len = 0;
                while ((len = bis.read(b)) != -1) {
                    bos.write(b, 0, len);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
        } catch (Exception e) {
            System.out.println("创建文件流异常：" + e.getMessage());
        } finally {
            // 关闭文件流
            try {
                if (null != bos) {
                    bos.close();
                    bos = null;
                }
                if (null != os) {
                    os.close();
                    os = null;
                }
                if (null != bis) {
                    bis.close();
                    bis = null;
                }
                if (null != fis) {
                    fis.close();
                    fis = null;
                }
            } catch (IOException e) {
                System.out.println("关闭文件流异常："+ e.getMessage());
            }
        }
    }
}
